<extend name="Base/common" />
<block name="body">
	<include file="Public/return_nav" />
	<footer class="ui-footer ui-footer-stable ui-border-t ui-btn-group ui-whitespace">
		<input type="hidden" value="1" id="step_value">
		<input type="hidden" value="{$data.order_id}" id="order_id">
		<a href="#" class="ui-btn-lg ui-btn-danger step">下一步</a>
	</footer>
	
	<section class="ui-container">
		<div class="ui-return-goods-wrap ui-whitespace">
			<h4 class="ui-border-b ui-return-goods-title">请选择要退货的商品</h4>
			<ul class="ui-list-return-goods choose_goods">
				<volist name="list" id="v">
				<li>
					<input type="hidden" id="cart_id" value="{$v.id}">
					<div class="ui-return-goods-info">
						<div class="goods-img"><img src="{$v.picture||get_cover='path'}" width="60" height="60"/></div>
						<div class="goods-info">
							<h4 class="ui-nowrap">{$v.name}</h4>
							<p><small class="ui-txt-info">数量：{$v.num}</small></p>
							<!-- <div class="count ui-tiled ui-border">
				        		<a href="#" class="move iconfont ui-border-r">&#xe768;</a>
				        		<span class="number number_{$v.id}" max_num="{$v.num}" price="{$v.price}">{$v.num}</span>
				        		<a href="#" class="add iconfont ui-border-l">&#xe767;</a>
				        	</div> -->
						</div>
						<div class="goods-price">￥{$v.price}</div>
					</div>
				</li>
				</volist>
				<li class="return_cause hide">
					<!-- <div class="all-price tr">
						<small>实付￥{$v.allprice}</small>
					</div> -->
					<div class="ui-form-item ui-form-item-show ui-form-item-link" >
			            <label for="#">退货原因</label>
			            <input type="text" id="return_msg" name="return_msg" value=""  autocomplete="off" readonly>
			        </div>
				</li>
			</ul>
		</div>
		<div class="ui-return-price-info ui-whitespace hide">
			<input type="hidden" id="all_return_price" value="{$data.all_goods_prices}">
			<h4 class="price-info"><span>退货商品金额</span><span>￥{$data.all_goods_prices}</span></h4>
			<p class="refund-info"><b>应退</b><b class="ui-txt-highlight">￥{$data.all_goods_prices}</b></p>
		    <p class="prompt-info">
			    <span>退回{$data.open_bank}</span>
			    <span>￥{$data.all_goods_prices}</span>
		    </p>
		</div>
		<!--s actionsheet -->
		<div class="ui-actionsheet">  
		  <div class="ui-actionsheet-cnt">
		    <h4>选择退货原因</h4> 
		    <button>脏污/掉色/划痕/掉漆/做工差</button>  
		    <button>商品规格不适合</button>
		    <button>商品性价比不高</button>
		    <button>与网站商品描述不符</button>
		    <button>包装/商品破损</button>
		    <button>物流慢/收货太迟</button>
		    <button class="ui-actionsheet-del">取消</button> 
		  </div>         
		</div><!--e actionsheet -->
	</section>
</block>
<block name="script">
	<script type="text/javascript">
		var temp_cart_id = new Array();
		Array.prototype.remove = function(val) {
            var index = this.indexOf(val);
            if (index > -1) {
                this.splice(index, 1);
            }
        };
		$('.choose_goods li div label input').tap(function(){
			var obj = $('.number_' + $(this).val());
			// alert($(this).attr('checked'));
			//判断复选框是否被选中
			if( $(this).attr('checked') == false ){
				temp_cart_id.push(Number($(this).val()));
				console.log(temp_cart_id);
				$(this).addClass('checked');
				$('.step').removeClass('disabled');	
				$('.number_' + $(this).val()).addClass('disabled');
				calcPrice($(this).val(), obj.attr('price'), obj.html(), true);
			}else{
				$(this).removeClass('checked');
				var count = $('.checked').length;
				calcPrice($(this).val(), obj.attr('price'), obj.html(), false);
				temp_cart_id.remove(Number($(this).val()));
				console.log(temp_cart_id);
				$('.number_' + $(this).val()).removeClass('disabled');
				if(count <= 0){
					$('.step').addClass('disabled');	
					//清空退款金额
					$('#all_return_price').val(0);
					voluation(0);
				}
			}
		});

		var load_num = 0;
		$('.step').tap(function(){
			var step_val = $('#step_value').val();
			// 第一步确定退货信息
			if(step_val == 1){
				//选择退货理由
				if( !$(this).hasClass('disabled') ){
					$('.ui-actionsheet').addClass('show');

				}	
			}
			//第二步生成退货订单信息
			var el = '';
			if(step_val == 2){
				var order_id = $('#order_id').val(),
					return_money = $('#all_return_price').val(),
					return_msg = $('#return_msg').val()
					cart_id = $('#cart_id').val();

				if( load_num >= 1 ){
					tipsInfo('请勿频繁操作！');
				}else{
					load_num = 1;
					$.ajax({
						url: "{:U('ReturnGoods/apply_return')}",
						type: 'POST',
						data: {
							order_id: order_id,
							return_money: return_money,
							return_msg: return_msg,
							cart_id: cart_id
						},
						dataType: 'json',
						error: function(){
							tipsInfo('连接服务器失败！');
							load_num = 0;
						},
						beforeSend:function(){
							load_num = 1;
							el = $.loading({content:'加载中...'})
						},
						success: function(r){
							if(r.status){
								load_num = 1;
								window.location.href = r.url;
							}else{
								load_num = 0;
								el.loading("hide");
								tipsInfo(r.info);
							}
						}
					});
				}
				
			}
		});

		//重选退货原因
		$('.return_cause div').tap(function(){
			$('.ui-actionsheet').addClass('show');
		});

		$('.ui-actionsheet div button').tap(function(){
			var return_msg = $(this).html();
			if(return_msg=='取消'){
				$('.ui-actionsheet').removeClass('show');
			}else{
				$('#step_value').val(2);
				$('#return_msg').val(return_msg);
				$('.ui-actionsheet').removeClass('show');
				$('.return_cause').removeClass('hide');
				$('.ui-return-price-info').removeClass('hide');
			}
		});
		
		$('.choose_goods li div .goods-info div a').tap(function(){
			var span_html = $(this).parent().find('span');
			var num = Number(span_html.html());
			var max_num = Number(span_html.attr('max_num'));
			var all_return_price = parseFloat($('#all_return_price').val()); 	//总金额
			var price = parseFloat(span_html.attr('price'));
			var new_all_return_price = 0;
			if($(this).hasClass('move')){
				if( num == 1){
					tipsInfo('商品数量不能再减少了');
	            	return false;
				}else{
					num = num - 1 <= 1 ? 1 : num - 1;	
					if(span_html.hasClass('disabled'))
						new_all_return_price = all_return_price - price;
				}
			}

			if($(this).hasClass('add')){
				if( num >= max_num ){
					tipsInfo('商品退货数量有误！');
					num = max_num;
					if(span_html.hasClass('disabled'))
						new_all_return_price = all_return_price;
				}else{
					num = num + 1;
					if(span_html.hasClass('disabled'))
						new_all_return_price = all_return_price + price;
				}
			}
			if(span_html.hasClass('disabled'))
				$('#all_return_price').val(new_all_return_price)
			voluation(new_all_return_price);
			span_html.html(num);

		});

		function tipsInfo(msg){
			$.tips({
		        content:msg,
		        stayTime:2000,
		        type:"success"
		    })
		}

		/**
		 * 赋值金额
		 * [voluation 赋值]
		 */
		function voluation(allPrice){
			$('.all-price small').html("实付￥" + allPrice);	//实付
			$('.price-info span').last().html("￥" + allPrice);	//退货商品金额
			$('.refund-info b').last().html("￥" + allPrice);	//应退
			$('.prompt-info span').last().html("￥" + allPrice);	//退回微信
		}

		/**
		 * [calcPrice 计算选择商品的退款价格]
		 * @param  {[type]} id    [ID]
		 * @param  {[type]} price [价格]
		 * @param  {[type]} num   [数量]
		 */
		function calcPrice(id,price,num, type){
			var all_return_price = parseFloat($('#all_return_price').val());
			if(type == true){
				all_return_price += parseFloat(price) * Number(num);
			}else{
				all_return_price -= parseFloat(price) * Number(num);
			}
			voluation(all_return_price);
			$('#all_return_price').val(all_return_price);
		}
	</script>
</block>